import { HospitalParams } from './../../../../api/demo/model/hospitalModel'
import { updateHospital } from './../../../../api/demo/hospital'
import { Switch } from 'ant-design-vue'
import { BasicColumn, FormSchema } from '/@/components/Table'
import { h } from 'vue'
import { useMessage } from '/@/hooks/web/useMessage'
export const columns: BasicColumn[] = [
  {
    title: '医院名',
    dataIndex: 'name',
    width: 120,
  },
  {
    title: '地址',
    dataIndex: 'address',
    width: 200,
  },
  {
    title: '联系电话',
    dataIndex: 'phone',
    width: 120,
  },
  {
    title: '等级',
    dataIndex: 'level',
    width: 120,
  },
  {
    title: '描述',
    dataIndex: 'description',
    width: 200,
  },
  {
    title: '注册时间',
    dataIndex: 'gmtCreate',
    width: 120,
  },
  {
    title: '状态',
    dataIndex: 'state',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false
      }
      return h(Switch, {
        checked: record.state === 0,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          const { createMessage } = useMessage()
          record.pendingStatus = true
          const newState = checked ? 0 : 1
          const newRecord: HospitalParams = JSON.parse(JSON.stringify(record))
          newRecord.creditCode = null
          newRecord.state = newState
          updateHospital(newRecord)
            .then(() => {
              record.state = newState
              createMessage.success('已成功修改医院状态')
            })
            .catch(() => {
              createMessage.error('修改医院状态失败')
            })
            .finally(() => {
              record.pendingStatus = false
            })
        },
      })
    },
  },
  {
    title: '社会凭证代码',
    dataIndex: 'creditCode',
    width: 120,
  },
]

export const searchFormScheme: FormSchema[] = [
  {
    field: 'name',
    label: '医院名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'level',
    label: '医院等级',
    component: 'Select',
    componentProps: {
      options: [
        { label: '一级医院', value: '一级医院' },
        { label: '二级医院', value: '二级医院' },
        { label: '三级医院', value: '三级医院' },
        { label: '其他', value: '其他' },
      ],
    },
    colProps: { span: 6 },
  },
  {
    field: 'state',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 },
      ],
    },
    colProps: { span: 6 },
  },
]

export const hospitalFormSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '医院名',
    field: 'name',
    component: 'Input',
    required: true,
  },
  {
    label: '医院地址',
    field: 'address',
    component: 'Input',
    required: true,
  },
  {
    label: '医院等级',
    field: 'level',
    required: true,
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '一级',
          value: '一级',
        },
        {
          label: '二级',
          value: '二级',
        },
        {
          label: '三级',
          value: '三级',
        },
        {
          label: '其他',
          value: '其他',
        },
      ],
    },
  },
  {
    label: '联系电话',
    field: 'phone',
    component: 'Input',
    required: true,
  },
  {
    label: '统一社会凭证码',
    field: 'creditCode',
    component: 'Input',
    required: true,
  },
  {
    required: true,
    field: 'state',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '启用', value: 0 },
        { label: '停用', value: 1 },
      ],
    },
  },
  {
    label: '描述',
    field: 'description',
    component: 'InputTextArea',
    required: true,
  },
]
